//
//  TransferPigListViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/9.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "TransferPigListViewController.h"
#import "transferManagerCell.h"

@interface TransferPigListViewController ()<UITableViewDelegate,UITableViewDataSource>

/**
 tableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 currentPage
 */
@property (nonatomic , assign) int currentPage;
/**
 limit
 */
@property (nonatomic , assign) int limit;
@end

@implementation TransferPigListViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void) initAction {
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArr removeAllObjects];
        self.currentPage = 1;
        [self dataRequest];
    }];
    [self.myTableView.mj_header beginRefreshing];
    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.currentPage ++;
        [self dataRequest];
    }];
    self.myTableView.mj_footer.hidden = YES;
}

- (void)initData {
    self.limit = 5;
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TransferManagerCell *cell = [TransferManagerCell cellWithTableView:tableView];
    if (self.dataArr.count > indexPath.row) {
        TransferManager *model = self.dataArr[indexPath.row];
        [cell configCellWithModel:model transferManagerType:TransferManagerTypePig];
        @weakify(self);
        [cell setCancelBlock:^{//撤销
            NSString *message = [NSString stringWithFormat:@"您确定要撤销编号为%@的仔猪吗？",model.pigNum];
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"仔猪撤销" message:message preferredStyle:UIAlertControllerStyleAlert];
            alertController.view.tintColor = [UIColor textBlackColor];
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
            
            UIAlertAction *confirmBtn = [UIAlertAction actionWithTitle:@"再想想" style:UIAlertActionStyleDefault handler:nil];
            [confirmBtn setValue:[UIColor primaryColor] forKey:@"_titleTextColor"];
            [alertController addAction:confirmBtn];
            
            UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"撤销" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                @strongify(self);
                [self cancelTransferRequestWithAssignmentId:model.assignmentId index:indexPath.row];
            }];
            [alertController addAction:cancelBtn];
        }];


    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark - target action

#pragma mark - private method
- (void)dataRequest {
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    TransferManagerParam *param = [[TransferManagerParam alloc] init];
    param.memberId = memberId;
    param.page = @(self.currentPage);
    param.limit = @(self.limit);
    param.type = @(TransferManagerTypePig);
    
    @weakify(self);
    [[[PigMarketApi sharedInstance] transferManagerWithParam:param] subscribeNext:^(TransferManagerList * _Nullable transferManagerList) {
        @strongify(self);
        
        [self.dataArr addObjectsFromArray:transferManagerList.list];
        [self.myTableView reloadData];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        if (self.currentPage == [transferManagerList.totalPage intValue] || [transferManagerList.totalPage intValue] == 0) {
            [self.myTableView.mj_footer endRefreshingWithNoMoreData];;
        } else {
            self.myTableView.mj_footer.hidden = NO;
        }
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
    }];
}
- (void)cancelTransferRequestWithAssignmentId:(NSNumber *)assignmentId index:(NSUInteger)index{
    
    @weakify(self);
    [[[PigMarketApi sharedInstance] cancelTransferWithAssignmentId:assignmentId] subscribeNext:^(NSNull * _Nullable x) {
        
        [self toastWithContent:@"撤销成功"];
        [self.dataArr removeObjectAtIndex:index];
        [self.myTableView reloadData];

    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
    }];
}
#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.rowHeight = 215;
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

@end
